/*
* @Author: ZHELIHAO\fangshuqin
* @Date:   2018-05-23 15:23:10
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-25 15:45:34
*/
{
	let EVENT_CLICK = Laya.Event.CLICK;
	class ShopBuy extends ui.Alert.ShopBuyUI{

		constructor(cb, bet, shopType, itemno, title, type, number_no, color, showpro,heroNum, maxNum) {
			super();
			this.callback = cb ;
			this.bet  = bet || 0;
			this.shopType = shopType;
			this.itemno = itemno || 0;
			this.title = title;
			this.type =  type ;
			this.number_no = number_no || 0;
			this.color = color || 2;

			// 进度条
			this.proshow = showpro || false;
			this.heroNum = heroNum || 0;
			this.maxNum  = maxNum  || 1;

			this.init();
		}

		init() {
		

			this.targetUrl ="";
			this.registerActions();
			this.initEvent();

			if(this.shopType == "diamondShop") {
				console.log("this.shopType", this.shopType, this.bet);
				Sail.io.emit(GAME_CMDS.RECHARGEDIAMOND, {"amount": this.bet});
			}


			if( this.shopType !== "dayShop" ) {
				this.pro_bg.visible = false;
				this.pro_bar.visible = false;
				this.dom_progress.visible = false;
			}else if(this.shopType === "dayShop" && this.proshow) {
				console.log("this.heroNum / this.maxNum", this.heroNum , this.maxNum) ;
				let percent = this.heroNum / this.maxNum;
                let per = Math.floor(percent * 100);
                let w = per / 100 * 424;
                w = w < 0 ? 0 : w;
                w = w > 424 ? 424 : w;
				this.dom_progress.scrollRect = {x : 0, y : 0, width : w, height : 41};

				this.dom_progress.text = this.heroNum + '/' + this.maxNum;
				this.pro_bg.visible = true;
				this.pro_bar.visible = true;
				this.dom_progress.visible = true;
			}

			// 弹层显示信息
			let itemno = this.itemno;
			this.dom_color.index = this.color;
			this.dom_title.text  =  this.title;
			this.dom_itemno.skin = `res/shop/${itemno}.png`;
			this.dom_type.index = this.type;
			this.dom_bet.text = this.bet;

		}

		 registerActions() {
            this.ACTIONS = {
                [GAME_CMDS.RECHARGEDIAMOND] :  this.ensureFn ,
                     
            };
            Sail.io.register(this.ACTIONS, this);
        }

		initEvent() {
			this.btn_confirm.on(EVENT_CLICK, this, ()=>{
				console.log("this.shopType",this.shopType);
				switch (this.shopType){
					case "diamondShop":
						window.location.href = this.targetUrl;
						this.onConfirm();
						break;

					case "spoilsShop":
						Sail.io.emit(GAME_CMDS.BUYGOODS,{"item_no": this.itemno ,"shop_type": this.shopType, "type": this.type, "number_no":  this.number_no});
						this.onConfirm();
						break;

					case "dayShop":
						Sail.io.emit(GAME_CMDS.BUYGOODS,{"item_no": this.itemno ,"shop_type": this.shopType, "type": this.type, "number_no":  this.number_no});
						this.onConfirm();
						break;

					case "coinShop":
						Sail.io.emit(GAME_CMDS.EXCHANGECOIN,{"coin": this.bet});
						this.onConfirm();
						break;
				}

			})

			this.btn_close.on(EVENT_CLICK, this, this.onConfirm);
		}


	    // 确定购买
	    ensureFn(data) {
	    	console.log("ensureFn", data);
	        // console.log("单个买");
	        let gameId = data.gameId;
	        let tradeName = data.tradeName;
	        let shuoldPay = this.bet;
	        let gameplatform = data.platform;
	        let gameorderid  = data.gameOrderId;
	        let currentUrl = data.redirect_uri;
	        console.log("shuoldPay",shuoldPay);
	        this.targetUrl = `/?act=payment&gameId=${gameId}&tradeName=${tradeName}&amount=${shuoldPay}&platform=${gameplatform}&gameOrderId=${gameorderid}&redirect_uri=${currentUrl}`;
	    }

	    onConfirm() {
	    	console.log("关闭")
	    	Sail.io.unregister(this.ACTIONS);
	    	this.close();
			this.callback && this.callback();
	/*		if(this.shopType === "spoilsShop") {
				console.log("战利品")
			}*/
			Sail.director.popScene(new Alert.SpoilsDetail());
	    }

		/*onClosed() {
			Sail.io.unregister(this.ACTIONS);
			this.callback && this.callback();
		}*/
	}

	Sail.class(ShopBuy, 'Alert.ShopBuy');
}